package com.algrithom.dfs;

import com.common.model.TreeNode;

/**
 * 572.另一棵树的子树
 */
public class Solution3 {
    
    public boolean isSubtree(TreeNode<Integer> s,TreeNode<Integer> t){
        if (t == null)
            return true;
        if (s == null)
            return false;
        return isSubtree(s.getLeftChild(),t) || isSubtree(s.getRightChild(),t) || isSameTree(s,t);
    }
    
    /**
     * 判断两棵树是否相同
     */
    public boolean isSameTree(TreeNode<Integer> s,TreeNode<Integer> t){
        if (s == null && t == null)
            return true;
        if (s == null || t == null)
            return false;
        if (!s.getValue().equals(t.getValue())) {
            return false;
        }
        return isSameTree(s.getLeftChild(),t.getLeftChild()) && isSameTree(s.getRightChild(),t.getRightChild());
    }
}
